package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController
{
    @Autowired
    private BusinessService businessService;
    @GetMapping
    public Result list(BusinessDto businessDto)
    {
        log.info( "查询所有业务: {}",businessDto);
        PageResult<Business> pageResult = businessService.listBusiness(businessDto);
        return Result.success(pageResult);
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Business business)
    {
        log.info("添加业务: {}",business);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(1);
        businessService.save(business);
        return Result.success();
    }
    @Log
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId)
    {
        log.info("分配业务: {},{}" , businessId, userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getBusiness(@PathVariable Integer id)
    {
        log.info("查询业务: {}" , id);
        Business business=businessService.getBusinessId(id);
        return Result.success(business);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Business business)
    {
        log.info("更新业务: {}",business);
        businessService.updateByBusiness(business);
        return Result.success();
    }
    @GetMapping("/pool")
    public Result listPool(BusinessDto businessDto)
    {
        log.info("查询所有待分配的业务");
        PageResult< Business> pageResult = businessService.listBusinessPool(businessDto);
        return Result.success(pageResult);
    }
    @Log
    @PutMapping("/back/{id}")
    public Result backpool(@PathVariable Integer id)
    {
        log.info("业务退回待分配: {}",id);
        businessService.backpool(id);
        return Result.success();
    }
    @Log
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id)
    {
        log.info("业务转客户: {}",id);
        businessService.toCustomerByid(id);
        return Result.success();
    }
}
